package test.service.contacts;

import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

/**
 * @author seunghoon
 * 서비스종료 예상 상황
 * 1. User 강제 종료 : Settings APP 를 통한 APP Manager 에서 서비스 강제 종료 - 브로드캐스트 리시버로 재시작 처리
 * 2. APP 버젼 업 : Service가 동작하는 APK 에 대한 버젼 업 될시 종료 - 다운로드 완료시 재시작 루틴 필요
 * 3. 단말 reboot : 단말기를 종료 후 재시작 하는 과정 - android.intent.action.BOOT_COMPLETED 리시버로 해결 가능
 * 4. Exception으로 인한 종료 : 해당 서비스에서 오류 발생 시 종료 - 오류 발생 경로를 차단, 다른 service에서 감시, 브로드캐스트 리시버로 재시작 처리
 * 5. 다른 APP 에서 서비스 강제 종료 : 마켓 등에 올라오는 다른 APP 에서 다른 서비스를 모두 종료시키는 루틴 실행 시 - 브로드캐스트 리시버로 재시작 처리
 *
 */

public class testServiceEX extends Service {

	private String TAG = "testServiceEX";
	private ContactsInfoGetter m_ContactsController;
	private ContentProviderInfoGetter m_APPCPController;
	//private CreateDBOpenHelpers m_DBhelpers;
	private BRReceiver BRReceiver;
	private boolean isStarted = false;
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		Log.d(TAG, "onCreate");
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		Log.d(TAG, "onDestroy");
		// TODO Auto-generated method stub
		unregisterReceiver(BRReceiver);
		super.onDestroy();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		Log.d(TAG, "onStart");
		super.onStart(intent, startId);

		Log.d(TAG, "action" + intent.getAction());
		
		if(intent.getAction().equals("testAction.CONTACTSLISTGETTER")){
			Log.d(TAG, "testAction.CONTACTSLISTGETTER");
			
			Intent Listgetterintent = new Intent();
			Bundle tmpbdl = new Bundle();
			tmpbdl.putSerializable("testputLIST", m_ContactsController.getContactsList());
			Listgetterintent.setClass(this, getContactsListActivity.class);
			Listgetterintent.putExtras(tmpbdl);
			PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, Listgetterintent, 0);
			try {
				pendingIntent.send();
			} catch (CanceledException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		else if(intent.getAction().equals("testAction.ADDCONTACT")){
			Log.d(TAG, "testAction.ADDCONTACT");
			m_ContactsController.addContact(intent.getStringExtra("ContactName"));
		}
		
		else if(intent.getAction().equals("testAction.APPLISTGETTERCALL")){
			Log.d(TAG, "testAction.APPLISTGETTERCALL");
			
			Intent Listgetterintent = new Intent();
			Bundle tmpbdl = new Bundle();
			tmpbdl.putSerializable("testputAPPLIST", m_APPCPController.getAPPList());
			Listgetterintent.setClass(this, testAPPListActivity.class);
			Listgetterintent.putExtras(tmpbdl);
			PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, Listgetterintent, 0);
			try {
				pendingIntent.send();
			} catch (CanceledException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		else if(intent.getAction().equals("testAction.ADDAPPENABLE")){
			Log.d(TAG, "testAction.ADDAPPENABLE");
			String strAPPname = intent.getStringExtra("APPNAME");
			String strIsEnable = "";
			if(intent.getBooleanExtra("ISENABLE", true))
				strIsEnable = intent.getStringExtra("ISENABLE");
			
			m_APPCPController.addAPP(strAPPname, strIsEnable);
	        
	        //m_DBhelpers.addDB(strAPPname, strIsEnable);
		}
		
		else if(intent.getAction().equals("testAction.SERVICESTART")){
			Log.d(TAG, "testAction.SERVICESTART");
			if(!isStarted){
				m_ContactsController = new ContactsInfoGetter(this.getContentResolver());
				m_APPCPController = new ContentProviderInfoGetter(this.getContentResolver());
		        //m_DBhelpers = new CreateDBOpenHelpers(this);
				
				BRReceiver = new BRReceiver();
				BRReceiver.serviceSetter(this);
				IntentFilter intentfilter = new IntentFilter();
				intentfilter.addAction(Intent.ACTION_BOOT_COMPLETED);
				intentfilter.addAction(Intent.ACTION_BATTERY_CHANGED);
				intentfilter.addAction(Intent.ACTION_SCREEN_ON);
				intentfilter.addAction(Intent.ACTION_SCREEN_OFF);
				registerReceiver(BRReceiver, intentfilter);
				
				isStarted = true;
			}
		}
		
		else if(intent.getAction().equals("testAction.SERVICERECRIVERSTART")){
			Log.d(TAG, "testAction.SERVICERECRIVERSTART");
			if(!isStarted){
				m_ContactsController = new ContactsInfoGetter(this.getContentResolver());
				m_APPCPController = new ContentProviderInfoGetter(this.getContentResolver());
		        //m_DBhelpers = new CreateDBOpenHelpers(this);
		        Object obj = intent.getExtras().get("testputBRRCV");
				if(obj instanceof BRReceiver){
					BRReceiver tmpOBJ = (BRReceiver)obj;
					tmpOBJ.serviceSetter(this);
				}
				isStarted = true;
			}
		}
		
		
		//mHandler.sendEmptyMessageAtTime(0, 60000);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	/*
	Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub

			if(m_service.isRestricted()){
				m_service.
			}
			else{
				mHandler.sendEmptyMessageDelayed(0, 60000);
			}
		}
	};*/
	

}
